package com.wn.www.carbeauty.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wn.www.carbeauty.entity.Orders;
import com.wn.www.carbeauty.entity.Services;
import com.wn.www.carbeauty.entity.dto.OrderDto;
import com.wn.www.carbeauty.mapper.OrdersMapper;
import com.wn.www.carbeauty.mapper.ServicesMapper;
import com.wn.www.carbeauty.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chan
 * @since 2025-04-11
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private ServicesMapper servicesMapper;

    public Object createOrderAndPay(OrderDto orderDto) {

        Orders orders = new Orders();
        orders.setCreatedAt(new Date());
        orders.setUpdatedAt(new Date());
        orders.setAppointment(new Date());

        Services services = servicesMapper.selectById(orderDto.getServicesId());
        orders.setPaymentAmount(services.getBasePrice()+"");
        String id = IdUtil.getSnowflakeNextId()+"";
        orders.setId(Integer.parseInt(id.substring(0,9)));
        orders.setUserId(1);//默认
        orders.setStoreServiceId(orderDto.getStoresId());
        orders.setSpecialNotes(orderDto.getSpecialNotes());
        orders.setStatus("pending");
        ordersMapper.insert(orders);

        return "支付";
    }

    @Override
    public void paySuccess(Integer id) {
        lambdaUpdate()
                .eq(Orders::getId,id)
                .set(Orders::getStatus,"completed")
                .update();
    }

    @Override
    public void payConfirmed(Integer id) {
        lambdaUpdate()
                .eq(Orders::getId,id)
                .set(Orders::getStatus,"confirmed")
                .update();
    }
}
